package com.hung.pro.iFlashCards;

import com.hung.pro.iFlashCards.bean.CardBean;
import com.hung.pro.iFlashCards.bean.LexiconBean;
import com.hung.pro.iFlashCards.component.CustomSizeLabel;
import com.hung.pro.iFlashCards.dao.CardBeanDao;
import com.hung.pro.iFlashCards.dao.LexiconBeanDao;
import com.hung.pro.iFlashCards.ui.Resources;
import com.hung.pro.iFlashCards.ui.UIHelper;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.uif_lite.panel.SimpleInternalFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.List;
import java.util.Random;

/**
 * Created by IntelliJ IDEA.
 * User: hung
 * Date: 2008/04/28
 * Time: 23:18:27
 * To change this template use File | Settings | File Templates.
 */
public class LearnWindow extends JFrame implements ActionListener {
    private JPanel learnPanel;
    private JButton answerButton;
    private JButton wrongButton;
    private JButton rightButton;
    private CustomSizeLabel wordLabel;
    private CustomSizeLabel readingLabel;
    private CustomSizeLabel meaningLabel;
    private CustomSizeLabel exampleLabel;
    private CustomSizeLabel noteLabel;
    /**
     * Lexicon combobox data model
     */
    private DefaultComboBoxModel lexiconComboDataModel = null;
    /**
     * lexicon combobox
     */
    JComboBox lexiconCombo = null;

    /**
     * learn type 0=>all, 1=>done lession, 2=>not done lession
     */
    private int lessonType = 0;

    /**
     * card data
     */
    private List<CardBean> cardList;
    /**
     * israndom
     */
    private boolean isRandom = false;

    /**
     * main panel, will contains toolbar, and learn panel
     */
    private JPanel mainPanel;

    /**
     * Toolbar
     */
    private JToolBar toolBar;
    private int currentLexiconId;
    private static CardBeanDao cardDao = new CardBeanDao();
    private static LexiconBeanDao lexiconDao = new LexiconBeanDao();
    private CardBean currentCard;

    public LearnWindow() {
        this.currentLexiconId = -1;
        mainPanel = new JPanel();
        buildLearnPanel();
        buildToolBar();
        buildMainPanel();
        this.setJMenuBar(buildMenu());

        loadCardsByLexicon(this.currentLexiconId);

    }

    private void buildMainPanel() {

        mainPanel.setLayout(new FormLayout("fill:d:grow", "center:34px:noGrow,top:4dlu:noGrow,center:d:grow"));
        CellConstraints cc = new CellConstraints();
        mainPanel.add(toolBar, cc.xy(1, 1, CellConstraints.FILL, CellConstraints.FILL));
        SimpleInternalFrame sif = new SimpleInternalFrame("Learn@pro");
        sif.setContent(learnPanel);
        mainPanel.add(sif, cc.xy(1, 3, CellConstraints.FILL, CellConstraints.FILL));
    }

    private void buildLearnPanel() {
        learnPanel = new JPanel();
        learnPanel.setLayout(new FormLayout("fill:max(d;4px):noGrow,left:4dlu:noGrow,fill:d:grow,left:4dlu:noGrow,fill:max(d;4px):noGrow,left:4dlu:noGrow,fill:max(d;4px):noGrow", "center:max(d;80px):grow,top:4dlu:noGrow,center:d:noGrow,top:4dlu:noGrow,center:max(d;60px):noGrow,top:4dlu:noGrow,center:d:noGrow,top:4dlu:noGrow,center:max(d;60px):noGrow,top:4dlu:noGrow,center:d:noGrow,top:4dlu:noGrow,center:max(d;60px):noGrow,top:d:noGrow,center:d:noGrow,top:d:noGrow,center:max(d;4px):noGrow,top:max(d;60px):noGrow,top:4dlu:noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,center:max(d;4px):noGrow,center:max(d;4px):noGrow"));
        learnPanel.setMinimumSize(new Dimension(500, 400));
        learnPanel.setPreferredSize(new Dimension(500, 400));
        wordLabel = new CustomSizeLabel("");
        wordLabel.setToolTipText(Resources.getString("tooltip.learnwindow.word"));
        CellConstraints cc = new CellConstraints();
        learnPanel.add(wordLabel, cc.xyw(1, 1, 7, CellConstraints.FILL, CellConstraints.FILL));
        final JSeparator separator1 = new JSeparator();
        learnPanel.add(separator1, cc.xyw(1, 4, 7, CellConstraints.FILL, CellConstraints.FILL));
        readingLabel = new CustomSizeLabel("");
        readingLabel.setToolTipText(Resources.getString("tooltip.learnwindow.reading"));
        learnPanel.add(readingLabel, cc.xyw(1, 5, 7, CellConstraints.FILL, CellConstraints.FILL));
        final JSeparator separator2 = new JSeparator();
        learnPanel.add(separator2, cc.xyw(1, 8, 7, CellConstraints.FILL, CellConstraints.FILL));
        meaningLabel = new CustomSizeLabel("");
        meaningLabel.setToolTipText(Resources.getString("tooltip.learnwindow.meaning"));
        learnPanel.add(meaningLabel, cc.xyw(1, 9, 7, CellConstraints.FILL, CellConstraints.FILL));
        final JSeparator separator3 = new JSeparator();
        learnPanel.add(separator3, cc.xyw(1, 12, 7, CellConstraints.FILL, CellConstraints.FILL));
        exampleLabel = new CustomSizeLabel("");
        exampleLabel.setToolTipText(Resources.getString("tooltip.learnwindow.example"));
        learnPanel.add(exampleLabel, cc.xyw(1, 13, 7, CellConstraints.FILL, CellConstraints.FILL));
        final JSeparator separator4 = new JSeparator();
        learnPanel.add(separator4, cc.xyw(1, 15, 7, CellConstraints.FILL, CellConstraints.FILL));
        noteLabel = new CustomSizeLabel("");
        noteLabel.setToolTipText(Resources.getString("tooltip.learnwindow.note"));
        learnPanel.add(noteLabel, cc.xyw(1, 18, 7, CellConstraints.FILL, CellConstraints.FILL));
        answerButton = new JButton();
        this.setButtonText(answerButton, Resources.getString("button.answer"));
        answerButton.addActionListener(new AnswerButtonActionListener());
        learnPanel.add(answerButton, cc.xy(3, 21, CellConstraints.RIGHT, CellConstraints.DEFAULT));
        wrongButton = new JButton();
        wrongButton.addActionListener(new WrongButtonActionListener());
        this.setButtonText(wrongButton, Resources.getString("button.wrong"));
        learnPanel.add(wrongButton, cc.xy(7, 21, CellConstraints.LEFT, CellConstraints.DEFAULT));
        rightButton = new JButton();
        rightButton.addActionListener(new RightButtonActionListener());
        this.setButtonText(rightButton, Resources.getString("button.right"));
        learnPanel.add(rightButton, cc.xy(5, 21, CellConstraints.LEFT, CellConstraints.DEFAULT));
    }

    private void buildToolBar() {
        toolBar = new JToolBar();
        toolBar.setFloatable(false);
        toolBar.putClientProperty("JToolBar.isRollover", Boolean.TRUE);
        //add button
        //quit button
        AbstractButton quitButton = UIHelper.createToolBarButton("quit.png", "Quit", Resources.getString("tooltip.learnwindow.quit"));
        quitButton.addActionListener(this);
        toolBar.add(quitButton);
        toolBar.addSeparator();

        //learn all button
        ButtonGroup group = new ButtonGroup();
        AbstractButton learnAllButton = UIHelper.createToolBarRadioButton("all.png", Resources.getString("tooltip.learnwindow.learnall"));
        learnAllButton.setSelectedIcon(UIHelper.readImageIcon("all_select.png"));
        learnAllButton.addActionListener(new LearnAllButtonActionListener());
        group.add(learnAllButton);
        learnAllButton.setSelected(true);
        toolBar.add(learnAllButton);

        AbstractButton learnOnlyDoneButton = UIHelper.createToolBarRadioButton("done.png", Resources.getString("tooltip.learnwindow.learndoneword"));
        learnOnlyDoneButton.setSelectedIcon(UIHelper.readImageIcon("done_select.png"));
        learnOnlyDoneButton.addActionListener(new LearnDoneButtonActionListener());
        group.add(learnOnlyDoneButton);
        toolBar.add(learnOnlyDoneButton);
        //not done button
        AbstractButton learnNotDoneButton = UIHelper.createToolBarRadioButton("not_done.png", Resources.getString("tooltip.learnwindow.learnnotdoneword"));
        learnNotDoneButton.setSelectedIcon(UIHelper.readImageIcon("not_done_select.png"));
        learnNotDoneButton.addActionListener(new LearnNotDoneButtonActionListner());
        group.add(learnNotDoneButton);
        toolBar.add(learnNotDoneButton);

        toolBar.addSeparator();
        //random button
        AbstractButton randomButton = UIHelper.createToolBarRadioButton("random.png", Resources.getString("tooltip.learnwindow.random"));
        randomButton.addActionListener(new RandomButtonActionListener());
        toolBar.add(randomButton);
        toolBar.addSeparator();
        //lexicon
        toolBar.add(new JLabel(Resources.getString("label.lexicon")));
        toolBar.add(Box.createHorizontalStrut(5));
        //lexicon combobox
        toolBar.add(buildLexiconCombobox());
        //add info button
        //make info button rightmost
        toolBar.add(Box.createHorizontalGlue());
        AbstractButton infoButton = UIHelper.createToolBarButton("info.png", "Info", Resources.getString("tooltip.learnwindow.info"));
        infoButton.addActionListener(new AboutMenuActionListener());
        toolBar.add(infoButton);


    }

    private JMenuBar buildMenu() {
        JMenuBar menuBar = new JMenuBar();
        //iFlashCards menu
        JMenu programMenu = new JMenu();
        UIHelper.setMenuItemText(programMenu, "i&FlashCards");
        //About iFlashCard menuitem
        programMenu.add(UIHelper.createMenuItem(Resources.getString("menu.about"), null, new AboutMenuActionListener()));
        programMenu.addSeparator();
        //Prereferences menu
        JMenu prereferenceMenu = new JMenu();
        UIHelper.setMenuItemText(prereferenceMenu, Resources.getString("menu.prereferences"));
        //Languages menu
        JMenu languageMenu = new JMenu();
        UIHelper.setMenuItemText(languageMenu, Resources.getString("menu.languages"));
        JMenuItem langEnMenu = UIHelper.createMenuItem(Resources.getString("menu.languages.en"), new LanguageActionListener());
        langEnMenu.setActionCommand("en");
        languageMenu.add(langEnMenu);

        JMenuItem langViMenu = UIHelper.createMenuItem(Resources.getString("menu.languages.vi"), new LanguageActionListener());
        langViMenu.setActionCommand("vi");
        languageMenu.add(langViMenu);
        JMenuItem langJaMenu = UIHelper.createMenuItem(Resources.getString("menu.languages.ja"), new LanguageActionListener());
        langJaMenu.setActionCommand("ja");
        languageMenu.add(langJaMenu);
        prereferenceMenu.add(languageMenu);
        programMenu.add(prereferenceMenu);
        //quit menu
        programMenu.add(UIHelper.createMenuItem(Resources.getString("menu.quit"), KeyStroke.getKeyStroke("ctrl Q"), this));
        menuBar.add(programMenu);
        //Lexicon menu
        JMenu lexiconMenu = new JMenu();
        UIHelper.setMenuItemText(lexiconMenu, Resources.getString("menu.lexicon"));
        lexiconMenu.add(UIHelper.createMenuItem(Resources.getString("menu.lexicon.edit"), KeyStroke.getKeyStroke("ctrl L"), new EditLexiconMenuActionListener()));
        menuBar.add(lexiconMenu);
        //Help menu
        //Vocabulary menu
        JMenu vocabularyMenu = new JMenu();
        UIHelper.setMenuItemText(vocabularyMenu, Resources.getString("menu.vocabulary"));
        vocabularyMenu.add(UIHelper.createMenuItem(Resources.getString("menu.vocabulary.edit"), KeyStroke.getKeyStroke("ctrl W"), new EditVocabularyMenuActionListener()));
        menuBar.add(vocabularyMenu);
        //help menu
        JMenu helpMenu = new JMenu();
        UIHelper.setMenuItemText(helpMenu, Resources.getString("menu.help"));
        helpMenu.add(UIHelper.createMenuItem(Resources.getString("menu.help.how"), KeyStroke.getKeyStroke("F1"), new HelpMenuActionListener()));
        menuBar.add(helpMenu);


        return menuBar;


    }

    /**
     * Build lexicon list combobox
     */
    private JComponent buildLexiconCombobox() {
        //Lexicon list
        try {

            List<LexiconBean> lexiconList = lexiconDao.getLexicon();
            lexiconComboDataModel = new DefaultComboBoxModel(lexiconList.toArray());
            lexiconCombo = new JComboBox(lexiconComboDataModel);
            lexiconCombo.setToolTipText(Resources.getString("tooltip.learnwindow.combo"));
            lexiconCombo.setFocusable(true);
            lexiconCombo.addActionListener(new LexiconComboActionListener());
            //update current lexiconId
            if (lexiconList.size() > 0) {
                if (currentLexiconId == -1) {
                    currentLexiconId = lexiconList.get(0).getId();
                } else {
                    //update selected index
                    int size = lexiconList.size();
                    for (int i = 0; i < size; i++) {
                        LexiconBean lexicon = lexiconList.get(i);
                        if (currentLexiconId == lexicon.getId()) {
                            lexiconCombo.setSelectedIndex(i);
                        }
                    }
                }
            }
            return lexiconCombo;

        } catch (Exception e) {
            String error = e.getLocalizedMessage();
            JOptionPane.showMessageDialog(mainPanel.getParent(), error, "Error", JOptionPane.WARNING_MESSAGE);
            e.printStackTrace();
        }
        //on error, return blank combo
        lexiconComboDataModel = new DefaultComboBoxModel();
        lexiconCombo = new JComboBox(lexiconComboDataModel);
        lexiconCombo.setFocusable(false);
        return lexiconCombo;
    }

     /**/
    /**
     * Redraw card
     *//*
    private void updateCard() {
    }*/
    private void createUIComponents() {
        // TODO: place custom component creation code here
    }

    /**
     * main
     */
    public static void main(String[] args) {
        configureUI();
        LearnWindow mainFrame = new LearnWindow();
        mainFrame.showWindow();


    }

    /**
     * Show window
     */
    public void showWindow() {
        this.setTitle("iFlashCards");
        this.setIconImage(UIHelper.readImageIcon("app.png").getImage());
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setContentPane(this.mainPanel);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        this.setSize(screenSize.width, screenSize.height - 100);
        //right button has focus first
        this.rightButton.requestFocusInWindow();
        //set locale
        this.setLocale(Resources.getLocale());
        this.setVisible(true);
    }

    private static void configureUI() {

        String selectedLaf = UIManager.getSystemLookAndFeelClassName();

        try {
            UIManager.setLookAndFeel(selectedLaf);
        } catch (Exception e) {
            System.out.println("Can't change L&F: " + e);
        }

    }

    /**
     * Load cards information that belonging to the specified lexicon, and display on table
     *
     * @param lexiconId
     */
    private void loadCardsByLexicon(int lexiconId) {
        try {
//load data from db
            cardList = cardDao.getCardsBelongingToLexicon(lexiconId);
            currentCard = null;
            CardBean card = getNextCard();
            displayWord(card);
        }
        catch (SQLException e) {
            String error = Resources.getString("message.loadcardsfailed");
            JOptionPane.showMessageDialog(mainPanel.getParent(), error, "Error", JOptionPane.WARNING_MESSAGE);
        }
        catch (Exception e) {
            String error = e.getLocalizedMessage();
            JOptionPane.showMessageDialog(mainPanel.getParent(), error, "Error", JOptionPane.WARNING_MESSAGE);
            e.printStackTrace();
        }

    }

    /**
     * Load cards information that belonging to the specified lexicon, and display on table
     *
     * @param lexiconId
     */
    private void loadCardsByLexiconWithStatus(int lexiconId, int status) {
        try {
//load data from db
            cardList = cardDao.getCardsBelongingToLexiconWithStatus(lexiconId, status);
            currentCard = null;
            CardBean card = getNextCard();
            displayWord(card);
        }
        catch (SQLException e) {
            String error = Resources.getString("message.loadcardsfailed");
            JOptionPane.showMessageDialog(mainPanel.getParent(), error, "Error", JOptionPane.WARNING_MESSAGE);
        }
        catch (Exception e) {
            String error = e.getLocalizedMessage();
            JOptionPane.showMessageDialog(mainPanel.getParent(), error, "Error", JOptionPane.WARNING_MESSAGE);
            e.printStackTrace();
        }

    }

    private void displayWord(CardBean card) {
        clearCard();
        if (card != null) {
            wordLabel.updateText(card.getWord());
        }
    }

    /**
     * display on window
     *
     * @param card
     */
    private void displayCard(CardBean card) {
        if (card == null) {
            clearCard();
            return;
        }
        wordLabel.updateText(card.getWord());
        readingLabel.updateText(card.getReading());
        meaningLabel.updateText(card.getMeaning());
        exampleLabel.updateText(card.getExample());
        noteLabel.updateText(card.getNote());
    }

    private void clearCard() {
        wordLabel.updateText("");
        readingLabel.updateText("");
        meaningLabel.updateText("");
        exampleLabel.updateText("");
        noteLabel.updateText("");
    }

    private CardBean getNextCard() {
        int size = cardList.size();
        if (size <= 0) {
            return null;
        }
        if (!isRandom && currentCard == null) {
            //get first card
            currentCard = cardList.get(0);
            return currentCard;
        }
        int nextIndex = 0;
        int currentIndex = currentCard == null ? -1 : cardList.indexOf(currentCard);
        if (isRandom) {
            Random randomMaker = new Random();
            nextIndex = randomMaker.nextInt(size);
            while (currentIndex == nextIndex) {
                nextIndex = randomMaker.nextInt(size);
            }
            currentCard = cardList.get(nextIndex);
            return currentCard;
        } else {
            nextIndex = currentIndex + 1 >= size ? 0 : ++currentIndex;
            currentCard = cardList.get(nextIndex);
            return currentCard;
        }
    }

    /**
     * @noinspection ALL
     */
    private void setButtonText(AbstractButton component, String text) {
        StringBuffer result = new StringBuffer();
        boolean haveMnemonic = false;
        char mnemonic = '\0';
        int mnemonicIndex = -1;
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) == '&') {
                i++;
                if (i == text.length()) break;
                if (!haveMnemonic && text.charAt(i) != '&') {
                    haveMnemonic = true;
                    mnemonic = text.charAt(i);
                    mnemonicIndex = result.length();
                }
            }
            result.append(text.charAt(i));
        }
        component.setText(result.toString());
        if (haveMnemonic) {
            component.setMnemonic(mnemonic);
            component.setDisplayedMnemonicIndex(mnemonicIndex);
        }
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return learnPanel;
    }

    /*************Listener***********************/
    /**
     * Learn all button
     */
    class LearnAllButtonActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            lessonType = LessonType.ALL;
            loadCardsByLexicon(currentLexiconId);

        }
    }

    /**
     * Learn only done word, the word that is marked as "you remembered"
     */
    class LearnDoneButtonActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            lessonType = LessonType.DONE_LESSION;
            loadCardsByLexiconWithStatus(currentLexiconId, CardBean.Status.REMEMBERED);

        }
    }

    /**
     * Learn only the words that you marked as "don't remember"
     */
    class LearnNotDoneButtonActionListner implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            lessonType = LessonType.NOT_DONE_LESSION;
            loadCardsByLexiconWithStatus(currentLexiconId, CardBean.Status.NOT_REMEMBERED);

        }
    }

    /**
     * Random button
     */
    class RandomButtonActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            JToggleButton randomButton = (JToggleButton) e.getSource();
            isRandom = randomButton.isSelected();
        }
    }

    /**
     * *********Action Listner***************
     */
    private class LexiconComboActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            JComboBox lexiconCombo = (JComboBox) e.getSource();
            LexiconBean lexicon = (LexiconBean) lexiconCombo.getSelectedItem();
            currentLexiconId = lexicon.getId();
            if (lessonType == LessonType.ALL) {
                loadCardsByLexicon(currentLexiconId);
            } else {
                loadCardsByLexiconWithStatus(currentLexiconId, lessonType);
            }
        }
    }

    /**
     * Answer button action listener
     */
    private class AnswerButtonActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            displayCard(currentCard);
        }
    }

    //Wrong Action Button Listener
    private class WrongButtonActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            updateWrongCard();
            displayWord(getNextCard());
        }
    }

    private class RightButtonActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            updateRightCard();
            displayWord(getNextCard());
        }
    }

    private class AboutMenuActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            AboutBox aboutWnd = new AboutBox();
            aboutWnd.showWindow();
        }
    }

    private class LanguageActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            JMenuItem menuItem = (JMenuItem) e.getSource();
            String actionCommand = menuItem.getActionCommand();
            //save language configuration
            Resources.setString("lang", actionCommand);
            String msg = Resources.getString("message.changedlang");
            JOptionPane.showMessageDialog(mainPanel.getParent(), msg, "iFlashCards", JOptionPane.INFORMATION_MESSAGE);
        }
    }

    private class EditLexiconMenuActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            ActionListener[] actions = lexiconCombo.getActionListeners();
            try {
                EditLexiconWindow editLexiconWnd = new EditLexiconWindow();
                editLexiconWnd.setModal(true);
                editLexiconWnd.showWindow();
                //remove combo action listener first
                if (actions.length > 0) {
                    lexiconCombo.removeActionListener(actions[0]);
                }
                //update lexicon list
                lexiconComboDataModel.removeAllElements();
                List<LexiconBean> lexiconList = lexiconDao.getLexicon();
                LexiconBean selectedLexicon = null;
                for (LexiconBean lexicon : lexiconList) {
                    if (lexicon.getId() == currentLexiconId) {
                        selectedLexicon = lexicon;
                    }
                    //add to list
                    lexiconComboDataModel.addElement(lexicon);
                }
                //check if currentLexicon is removed
                if (selectedLexicon == null && lexiconList.size() > 0) {
                    selectedLexicon = lexiconList.get(0);
                    currentLexiconId = selectedLexicon.getId();
                }
                if (selectedLexicon != null) {
                    lexiconComboDataModel.setSelectedItem(selectedLexicon);
                }
                //relooad cards
                if (lessonType == LessonType.ALL) {
                    loadCardsByLexicon(currentLexiconId);
                } else {
                    loadCardsByLexiconWithStatus(currentLexiconId, lessonType);
                }
                lexiconCombo.addActionListener(actions[0]);

            } catch (Exception e1) {
                String error = Resources.getString("message.invokelexiconeditorfailed");
                JOptionPane.showMessageDialog(mainPanel.getParent(), error, "Error", JOptionPane.WARNING_MESSAGE);
                e1.printStackTrace();
            }
        }
    }

    private class EditVocabularyMenuActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            try {
                EditVocabularyWindow editVocabularyWnd = new EditVocabularyWindow();
                editVocabularyWnd.setCurrentLexiconId(currentLexiconId);
                editVocabularyWnd.setModal(true);
                editVocabularyWnd.showWindow();

            } catch (Exception e1) {
                String error = Resources.getString("message.invokevocabeditorfailed");
                JOptionPane.showMessageDialog(mainPanel.getParent(), error, "Error", JOptionPane.WARNING_MESSAGE);
            }
        }
    }

    private class HelpMenuActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
             String msg = Resources.getString("message.learnwindow.help");
            JOptionPane.showMessageDialog(mainPanel.getParent(), msg, "iFlashCards", JOptionPane.INFORMATION_MESSAGE, UIHelper.readImageIcon("app.png"));
        }
    }

    private void updateRightCard() {
        try {
            CardBean card = currentCard;
            if (card == null) {
                return; //no card to update
            }
            card.setStatus(CardBean.Status.REMEMBERED);
            cardDao.updateCardStatus(card);
        } catch (Exception e) {
            String error = Resources.getString("message.updatecardstatfailed");
            JOptionPane.showMessageDialog(mainPanel.getParent(), error, "Error", JOptionPane.WARNING_MESSAGE);
            e.printStackTrace();
        }
    }

    /**
     * update card as "not-remembered"
     */

    private void updateWrongCard() {
        try {
            CardBean card = currentCard;
            if (card == null) {
                return; //no card to update
            }
            card.setStatus(CardBean.Status.NOT_REMEMBERED);
            cardDao.updateCardStatus(card);
        } catch (Exception e) {
            String error = Resources.getString("message.updatecardstatfailed");
            JOptionPane.showMessageDialog(mainPanel.getParent(), error, "Error", JOptionPane.WARNING_MESSAGE);
            e.printStackTrace();
        }
    }

    /**
     * Lession type 0 => all, 1=>done, 2=>not done
     * used to define state of lession type
     */
    private interface LessonType {
        public static final int ALL = 0;
        public static final int DONE_LESSION = 1;
        public static final int NOT_DONE_LESSION = 2;
    }

    /**
     * Close window
     *
     * @param e
     */
    public void actionPerformed(ActionEvent e) {
        //close the application
        dispose();
    }
}
